package com.leo.ibatis.demo.web.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import com.leo.ibatis.demo.criteria.CategoryCriteria;
import com.leo.ibatis.demo.domain.Category;
import com.leo.ibatis.demo.service.CategoryService;

public class CategoryController extends MultiActionController {

	private CategoryService categoryService;

	public void setCategoryService(CategoryService categoryService) {
		this.categoryService = categoryService;
	}

	public ModelAndView list(HttpServletRequest request, HttpServletResponse response) throws Exception {
		CategoryCriteria categoryCriteria = new CategoryCriteria();
		bind(request, categoryCriteria);
		
		List<Category> categoryList = categoryService.listOnPage(categoryCriteria);
		
		ModelAndView mav = new ModelAndView("category/list");
		mav.addObject("categoryList", categoryList);
		mav.addObject("categoryCriteria", categoryCriteria);
		
		return mav;
	}

	public ModelAndView detail(HttpServletRequest request, HttpServletResponse response) throws Exception {
		CategoryCriteria categoryCriteria = new CategoryCriteria();
		bind(request, categoryCriteria);
			
		String catid = ServletRequestUtils.getRequiredStringParameter(request, "catid");

		Category category = categoryService.select(catid);
		
		ModelAndView mav = new ModelAndView("category/detail");
		mav.addObject("category", category);
		mav.addObject("categoryCriteria", categoryCriteria);
		
		return mav;
	}

	public ModelAndView delete(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String catid = ServletRequestUtils.getRequiredStringParameter(request, "catid");

		categoryService.delete(catid);
		
		return new ModelAndView("redirect:category.do");
	}

}
